/*
 * file.c
 *
 *  Created on: Jun 21, 2011
 *      Author: sainw
 */
#define _XOPEN_SOURCE 500	/* strptime() */
#define _BSD_SOURCE			/* scandir(), timegm() */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <linux/limits.h>
#include <netdb.h>
#include <ctype.h>
#include <errno.h>
#include <dlfcn.h>
#include <time.h>
#include "file.h"

static const char * file_mime_lookup(const char *path) {
	struct mimetype *m = &mime_types[0];
	const char *e;

	while (m->extn) {
		e = &path[strlen(path) - 1];

		while (e >= path) {
			if ((*e == '.') && !strcasecmp(&e[1], m->extn))
				return m->mime;

			e--;
		}

		m++;
	}

	return "application/octet-stream";
}

static char * file_unix2date(time_t ts) {
	  struct tm * timeinfo;
	  int year, month ,day;
	  char * weekday[] = { "Sunday", "Monday",
	                       "Tuesday", "Wednesday",
	                       "Thursday", "Friday", "Saturday"};

	static char str[128];

	if(ts==NULL) {
			fprintf(stderr, "null time");

	}
	timeinfo= gmtime(&ts);

	strftime(str, sizeof(str), "%a, %d %b %Y %H:%M:%S GMT", timeinfo);
	printf("date:%s\n",str);

  printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
	return str;
}

static int scandir_filter_dir(const struct dirent *e) {
//	fprintf(stderr, "d_name:%s\n",e->d_name);

	return strcmp(e->d_name, ".") ? 1 : 0;
}

void scan_dir(const char *path) {
	int i;
	int count = 0;
	char filename[PATH_MAX];
	char *pathptr;
	struct dirent **files = NULL;
	struct stat s;

	if ((count = scandir(path, &files, scandir_filter_dir, alphasort)) > 0) {
		memset(filename, 0, sizeof(filename));
		fprintf(stderr, "path:%s\n",path);
		fprintf(stderr, "file:%s\n",filename);
		fprintf(stderr, "size file:%d\n",sizeof(filename));
		memcpy(filename, path, sizeof(filename));
		fprintf(stderr, "size file:%d\n",sizeof(filename));
		pathptr = &filename[strlen(filename)];

		/* list subdirs */
		for (i = 0; i < count; i++) {
			strncat(filename, files[i]->d_name,
					sizeof(filename) - strlen(files[i]->d_name));

			if (!stat(filename, &s) && (s.st_mode & S_IFDIR) && (s.st_mode
					& S_IXOTH))
				printf("name:%s,%s,%s,%s, %.02f kbyte\n", "pi->name",
						files[i]->d_name, files[i]->d_name,
						file_unix2date(s.st_mtime), s.st_size / 1024.0);

			*pathptr = 0;
		}

		/* list files */
		for (i = 0; i < count; i++) {
			strncat(filename, files[i]->d_name,
					sizeof(filename) - strlen(files[i]->d_name));

			if (!stat(filename, &s) && !(s.st_mode & S_IFDIR) && (s.st_mode
					& S_IROTH))
				printf("name:%s,%s,%s,%s,%s,%.02f kbyte\n", "pi->name",
						files[i]->d_name, files[i]->d_name,
						file_unix2date(s.st_mtime), file_mime_lookup(filename),
						s.st_size / 1024.0);

			*pathptr = 0;
		}
	}
	out: if (files) {
		for (i = 0; i < count; i++)
			free(files[i]);

		free(files);
	}
}

void main() {
//	  char buf1[80], buf2[80];
//
//	    strcpy(buf1, "When, in the course of...");
//	    memcpy(buf2, buf1, 80);
//	    printf(buf2);
//
//	printf("debug2");
//	char filename[4096];
//	fprintf(stderr,"debug1 %d,%d" ,PATH_MAX ,sizeof(filename));
//
//	const char *ptr="testing";
//	fprintf("t:%s",ptr);
//	memset(filename, 0, sizeof(filename));
//	memcpy(filename, ptr, sizeof(filename));
//
//	fprintf(stderr,"debug1 %d,%d" ,PATH_MAX ,sizeof(filename));
	char dir[PATH_MAX]="/home/sainw/";
//	char *p;
//	p=dir;
//	*p="/home/sainw";

	scan_dir(dir);
	struct dirent **namelist;
	int n;

//	n = scandir("/", &namelist, 0, alphasort);
//	if (n < 0)
//		perror("scandir");
//	else {
//		while (n--) {
//			printf("%s\n", namelist[n]->d_name);
//			free(namelist[n]);
//		}
//		free(namelist);
//	}
}

